﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using AGMLite.ViewModelClass;
using AGMLite.UserControl;
using AGMLite.Windows;
using System.Collections.Specialized;
using AGMLite.BaseClass;
using Hardcodet.Wpf.TaskbarNotification;
using System.Windows.Threading;
using AGMLite.Windows.Notify;
using System.Windows.Controls.Primitives;

namespace AGMLite
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private AGMProjects _agmProjects;
        private ProjectsViewModel m_projectsVM;

        public MainWindow()
        {
            InitializeComponent();
            this.StateChanged += new EventHandler(Window_StateChanged);

            m_projectsVM = new ProjectsViewModel();
            this.DataContext = m_projectsVM;
            
            LoadingProjects();
        }

        void Window_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                ShowMinimizeToTrayMessage();
            }
        } 

        private void LoadingProjects()
        {
            _agmProjects = new AGMProjects();
            _agmProjects.Load();
            ObservableCollectionEx<ProjectViewModel> projectVM = new ObservableCollectionEx<ProjectViewModel>();
            foreach (AGMProject project in _agmProjects.ProjectCollection)
            {
                projectVM.Add(new ProjectViewModel(project));
            }
            m_projectsVM.ProjectCollection = projectVM;

            // No any project, then create one
            if (_agmProjects.ProjectCollection.Count == 0)
                m_projectsVM.Add();

            foreach (AGMProject project in _agmProjects.ProjectCollection)
            {
                project.TaskBarIcon = AGMLiteNotifyIcon;
                project.Connect();                
            }
        }        

        private void DisconnectProject()
        {
            _agmProjects.DisconnectAll();
        }

        protected override void OnStateChanged(EventArgs e)
        {
            if (WindowState == System.Windows.WindowState.Minimized)
                this.Hide();

            base.OnStateChanged(e);
        }      

        private void Window_Closed(object sender, EventArgs e)
        {
            DisconnectProject();
            AGMLiteNotifyIcon.Dispose();
        }

        private void mainTab_Loaded(object sender, RoutedEventArgs e)
        {
            if (mainTab.Items.Count > 0)
                mainTab.SelectedIndex = 0;
        }

        private void miExit_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }

        private void AGMLiteNotifyIcon_TrayMouseDoubleClick(object sender, RoutedEventArgs e)
        {
            this.Show();
            this.Activate();
            this.WindowState = WindowState.Normal;
        }

        private void miShow_Click(object sender, RoutedEventArgs e)
        {
            this.Show();
            this.Activate();
            this.WindowState = WindowState.Normal;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
            ShowMinimizeToTrayMessage();
        }

        private void ShowMinimizeToTrayMessage()
        {
            FancyBalloon balloon = new FancyBalloon();
            balloon.BalloonTitle = "HP AGM Lite";
            balloon.BalloonText = "AGM Lite is still running";
            balloon.BalloonIcon = @"/images/agm.png";
            balloon.DoubleClickEvent += new RoutedEventHandler(balloon_DoubleClickEvent);
            //show balloon and close it after 4 seconds
            AGMLiteNotifyIcon.ShowCustomBalloon(balloon, PopupAnimation.Slide, 4000);
        }

        private void balloon_DoubleClickEvent(object sender, RoutedEventArgs e)
        {
            this.Show();
            this.Activate();
            this.WindowState = WindowState.Normal;
        }
    }

    public class ProjectIconConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Random rnd = new Random();
            BitmapImage img = new BitmapImage(); ;
            string imgPath = System.IO.Path.Combine(Common.ApplicationSetting.SettingStoreFolder, "ProjectIcons",  value.ToString());
            if (System.IO.File.Exists(imgPath))
                img = new BitmapImage(new Uri(imgPath));
            else
                img = new BitmapImage(new Uri(string.Format("pack://application:,,,/AGMLite;component/images/project/{0}.png", rnd.Next(1, 12))));

            return img;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ProjectStatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            BitmapImage img = new BitmapImage();
            int status = (int)value;

            switch (status)
            {
                case 0:
                    img = new BitmapImage(new Uri("pack://application:,,,/AGMLite;component/images/disconnect.png"));
                    break;
                case 1:
                    img = new BitmapImage(new Uri("pack://application:,,,/AGMLite;component/images/connecting.png"));
                    break;
                case 2:
                    img = new BitmapImage(new Uri("pack://application:,,,/AGMLite;component/images/connected.png"));
                    break;
            }

            return img;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
